using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for ReportControl.
	/// </summary>
	public class ReportControl : System.Windows.Forms.UserControl
	{
    private ExtendedDataGrid resultGrid;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label nameLabel;
    private System.Windows.Forms.Label descLabel;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public ReportControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.resultGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.panel1 = new System.Windows.Forms.Panel();
      this.descLabel = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.Label();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.resultGrid)).BeginInit();
      this.panel1.SuspendLayout();
      this.SuspendLayout();
      // 
      // resultGrid
      // 
      this.resultGrid.AllowNavigation = false;
      this.resultGrid.CaptionVisible = false;
      this.resultGrid.DataMember = "";
      this.resultGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.resultGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.resultGrid.Location = new System.Drawing.Point(0, 104);
      this.resultGrid.Name = "resultGrid";
      this.resultGrid.ReadOnly = true;
      this.resultGrid.Size = new System.Drawing.Size(578, 296);
      this.resultGrid.TabIndex = 0;
      this.resultGrid.OpenSelected += new System.EventHandler(this.resultGrid_OpenSelected);
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.descLabel);
      this.panel1.Controls.Add(this.nameLabel);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(578, 104);
      this.panel1.TabIndex = 1;
      // 
      // descLabel
      // 
      this.descLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                  | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.descLabel.Location = new System.Drawing.Point(16, 40);
      this.descLabel.Name = "descLabel";
      this.descLabel.Size = new System.Drawing.Size(554, 56);
      this.descLabel.TabIndex = 3;
      this.descLabel.Text = "descLabel";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.nameLabel.Location = new System.Drawing.Point(8, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.Size = new System.Drawing.Size(560, 23);
      this.nameLabel.TabIndex = 1;
      this.nameLabel.Text = "nameLabel";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(578, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 400);
      this.uiActionsControl.TabIndex = 2;
      // 
      // ReportControl
      // 
      this.Controls.Add(this.resultGrid);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "ReportControl";
      this.Size = new System.Drawing.Size(728, 400);
      ((System.ComponentModel.ISupportInitialize)(this.resultGrid)).EndInit();
      this.panel1.ResumeLayout(false);
      this.ResumeLayout(false);

    }
		#endregion

    private Report report;
    public void LoadReport(Report report)
    {
      this.report = report;
      nameLabel.Text = report.Name;
      descLabel.Text = report.Description;

      DataTable results = report.ResultsDataTable;

      resultGrid.ClearColumns();
      for (int i = 0; i < results.Columns.Count; i++)
      {
        DataColumn col = results.Columns[i];
        resultGrid.AddColumn(col.ColumnName);
        if (col.DataType == typeof(DateTime))
          resultGrid.SetFormat(col.ColumnName, "G");
      }

      resultGrid.DataSource = results;

      resultGrid.AutosizeColumns();
      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the report grid", new ActionExecuteDelegate(RefreshReport));
      if (results.Columns.Contains("Folder ID"))
        actionList.Add("Open selected folders", "Open the selected folders in the grid", 
          new ActionExecuteDelegate(OpenSelected));
      actionList.Add("Copy to clipboard", "Copy the report results to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actionList;
    }

    private string CopyToClipboard()
    {
      resultGrid.CopyToClipboard();
      return null;
    }

    private string RefreshReport()
    {
      using (new CursorHandler())
      {
        resultGrid.DataSource = null;
        resultGrid.DataSource = report.ResultsDataTable;
      }
      return null;
    }

    private string OpenSelected()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)resultGrid.BindingContext[resultGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(resultGrid.IsSelected(i))
          {
            DataRowView row = (DataRowView)bm.List[i];
            if (row.Row.Table.Columns.Contains("Folder ID"))
            {
              string folderId = row.Row["Folder ID"].ToString();
              Folder folder = new Folder(report.Server, folderId);
              FolderForm form = new FolderForm();
              form.LoadFolder(folder);
              form.Show();
            }
          }
        }
      }
      return null;
    }

    private void resultGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenSelected();
    }
	}
}
